frameworks.business.BaseBroker = function(){
	this.httpService = new frameworks.business.HttpReqService();
	//this.stack = new RequestStack();
};
frameworks.business.BaseBroker.prototype = {};



frameworks.business.RequestItem = function(label,connect,method,arguments,result,modal){
	frameworks.widgets.BaseStackItem.call(this);
	this.label = label;
	this.connect = connect;
	this.method = method;
	this.arguments = arguments;
	this.result = result;
	this.modal = modal;
};
frameworks.business.RequestItem.prototype = new frameworks.widgets.BaseStackItem();
frameworks.business.RequestItem.prototype.__super = frameworks.widgets.BaseStackItem;
frameworks.business.RequestItem.prototype.execute = function(){	this.connect.setRequest(this);};
frameworks.business.RequestItem.prototype.setErrorData = function(result){
	if(this.errorCallBack.length == 0){
		this.addErrorCallback(Util.GlobalErrorCallback);
	}
	this.__super.prototype.setErrorData(result);
}



frameworks.business.RequestConnect = function(){
	this._request = null;
};
frameworks.business.RequestConnect.prototype = {
	setRequest : function(item){
		if(this._request == null){
			this._request = item;
			this[this._request.method].apply(this, item.arguments);	
		}
	},
	connect : function(){
		
	},
	connectProgress : function(data){
		
	},
	connectSuccess : function(code){
		if(typeof code === "string"){
			var e, str=code, arr=str.split("êêê");
			try{
				code = JSON.parse(arr[0]);
				if(this._request.result && code.valid){
					code.data = new this._request.result(code.data);
				}
				arr.shift();
				code.attachment = arr;
			}catch(e){
				code = {};
				code.errors = {p01 : this._request.label + ":" + Language.LoadErrorFormat};
			}
			code.source = str;
		}
		var result = new frameworks.business.RequestResult(code);
		if(result.valid){
			this._request.setSuccessData(result);
		}else{
			this._request.setErrorData(result);
		}
		this._request = null;
	},
	connectError : function(code){
		if(typeof code === "string"){
			code = {};
			code.errors = {p01 : code};
		}
		var result = new frameworks.business.RequestResult(code);
		Util.Alert(this._request.label + result.formatErrorMessage() + Language.LoadRetryLabel, Language.ErrorTitlePop);
	}
};

frameworks.business.HttpReqService = function(){
	frameworks.business.RequestConnect.call(this);
};
frameworks.business.HttpReqService.prototype = new frameworks.business.RequestConnect();
frameworks.business.HttpReqService.prototype.ajax = function(url,get,post,timeout,cache){
	var type = (post == null)?"get":"post";
	if(get != null){url += ((url.indexOf("?") > -1) ? "&" : "?")+$.param(get);}
	var that = this;
	$.ajax({ type:type, url:url, data:post, timeout:timeout, cache:cache,
        success: function (code, textStatus) {
        	that.connectSuccess(code);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
        	that.connectError([{nid:'framework', eid:'remoteError', file:'', line:0 , note:''}]);
        }
    });
};
frameworks.business.HttpReqService.prototype.configGet = function(){
	this.ajax(Setting.ConfigUrl);
};

frameworks.business.RequestResult = function(code){
	frameworks.widgets.Result.call(this,code);
};
frameworks.business.RequestResult.prototype = new frameworks.widgets.Result();
frameworks.business.RequestResult.prototype.__super = frameworks.widgets.Result;
frameworks.business.RequestResult.prototype.decode = function(code){
	this.__super.prototype.decode.call(this, code);
};
